package com.phuoc.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.phuoc.entities.TypeOfEstablishment;

public class TypeOfEstablishmentDAO {
	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private String[] allCols = { 
			MySQLiteHelper.TYPE_ESTAB_ID,
			MySQLiteHelper.TYPE_ESTAB_NAME};
	
	public TypeOfEstablishmentDAO(Context context){
		dbHelper = new MySQLiteHelper(context);
	}
	
	// Open connection
	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	// Close connection
	public void close() {
		dbHelper.close();
	}
	
	// Create type of establishment
	public void createTypeOfEstablishment(String name) {
		// ContentValues to hold our establishment
		ContentValues values = new ContentValues();
		
		// Add a value to a set
		values.put(MySQLiteHelper.TYPE_ESTAB_NAME, name);

		database.insert(MySQLiteHelper.TABLE_TYPE_OF_ESTABLISHMENT, null, values);
	}
	
	// Get all name type of establishment
	public ArrayList<String> getAllTypeOfEstablishs(){
		ArrayList<String> nameTypeEstabs = new ArrayList<String>();
		Cursor cs = database.query(MySQLiteHelper.TABLE_TYPE_OF_ESTABLISHMENT, allCols, null, null, null, null, null);
		while(cs.moveToNext()){
			nameTypeEstabs.add(cursorToTypeOfEstab(cs).getName());
		}
		cs.close();
		return nameTypeEstabs;
	}
	
	// Checking name type of Establishment is exists ?
	public boolean checkNameExists(String name){
	Cursor cs =	database.query(MySQLiteHelper.TABLE_TYPE_OF_ESTABLISHMENT, 
			allCols, MySQLiteHelper.TYPE_ESTAB_NAME + " = '" + name + "'", null, null, null, null);
	
		if(cs.getCount() > 0){
			cs.close();
			return true;
		}else{
			cs.close();
			return false;
		}
	}
	
	// Get id by name
	public long getIdByName(String name){
		Cursor cs =	database.query(MySQLiteHelper.TABLE_TYPE_OF_ESTABLISHMENT, 
				allCols, MySQLiteHelper.TYPE_ESTAB_NAME + " = '" + name + "'", null, null, null, null);
		
		if(cs.getCount() > 0){
			cs.moveToFirst();
			TypeOfEstablishment typeEstab = cursorToTypeOfEstab(cs);
			cs.close();
			return typeEstab.getId();
		}else{
			cs.close();
			return -1;
		}
	}
	
	// get Name by Id
	public String getNameById(long id){
		Cursor cs =	database.query(MySQLiteHelper.TABLE_TYPE_OF_ESTABLISHMENT, 
				allCols, MySQLiteHelper.TYPE_ESTAB_ID + " = " + id, null, null, null, null);
		cs.moveToFirst();
		String typeEstab = cursorToTypeOfEstab(cs).getName();
		cs.close();
		return typeEstab;
	}
	
	// convert cursor to type of establishment
	public TypeOfEstablishment cursorToTypeOfEstab(Cursor cs){
		TypeOfEstablishment typeEstab = new TypeOfEstablishment();
		typeEstab.setId(cs.getLong(0));
		typeEstab.setName(cs.getString(1));
		
		return typeEstab;
	}
}
